package com.dy.nygl.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.dy.nygl.pojo.UserInfo;
import com.dy.nygl.service.UserInfoService;

@Controller
@RequestMapping("/userInfo")
public class UserInfoController {

	private int id;
	
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Resource(name="userInfoService")
	private UserInfoService userInfoService;
	
	@RequestMapping("/setUserInfo.action")
	public ModelAndView updateByName(HttpServletRequest request,HttpSession session)throws ServletException,IOException{
		String name=request.getParameter("name");
		String tel=request.getParameter("tel");
		String department=request.getParameter("department");
		String job=request.getParameter("job");
		
		System.err.println("---->get:"+name);
		
		UserInfo userInfo=new UserInfo();
		System.err.println("Controller----"+getId());
		userInfo.setId(getId());
		userInfo.setName(name.trim());
		userInfo.setTel(tel.trim());
		userInfo.setDepartment(department.trim());
		userInfo.setJob(job.trim());	
		userInfoService.updateInfo(userInfo);
		
		ModelAndView model=new ModelAndView();
		model.setViewName("Successful");
		return model;
	}
	
	@RequestMapping("/selectById.action")
	public void selectById(@Param("id")String id1,HttpServletResponse response) throws IOException{
		int id=Integer.parseInt(id1);
		System.out.println("---->getId:"+id);
		setId(id);
	    PrintWriter out=response.getWriter();
	    out.print(3);
	}
	
	@RequestMapping("/selectByAccount.action")
	public void selectByName(@Param("name")String name,HttpServletResponse response) throws IOException{
		UserInfo userInfo=userInfoService.selectByName(name);
	    setId(userInfo.getId());
	    System.err.println("------->action------");
	    PrintWriter out=response.getWriter();
	    out.print(3);
	}
	
	@RequestMapping("/insertInfo.action")
	public void insertInfo(){
		String name="666";
		UserInfo userInfo2=userInfoService.selectByName(name);
		if(userInfo2!=null){
			System.err.println("---->UserInfo Has Been Insert");
		}else{
			UserInfo userInfo=new UserInfo();
			userInfo.setName(name);
			userInfoService.insertInfo(userInfo);
		}
		
	}
}
